package cn.bluech.template.ui

import android.animation.ObjectAnimator
import android.annotation.TargetApi
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.os.Build
import android.os.Bundle
import android.view.ViewTreeObserver
import android.view.Window
import android.view.animation.DecelerateInterpolator
import androidx.annotation.RequiresApi
import cn.bluech.template.R
import cn.bluech.template.ui.base.BaseActivity
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.activity_full_image.*

/**
 */
class FullImageActivity : BaseActivity() {
    private var left = 0
    private var top = 0
    private var scaleX = 0f
    private var scaleY = 0f
    private var background: Drawable? = null
    private var iSrcX = 0
    private var iSrcY = 0
    private var iSrcWidth = 0
    private var iSrcHeight = 0
    private var iSrcUri: String? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        setContentView(R.layout.activity_full_image)
        iSrcX = intent.getIntExtra("x", 0)
        iSrcY = intent.getIntExtra("y", 0)
        iSrcWidth = intent.getIntExtra("width", 0)
        iSrcHeight = intent.getIntExtra("height", 0)
        iSrcUri = intent.getStringExtra("uri")

        initView()
    }

    @RequiresApi(Build.VERSION_CODES.JELLY_BEAN)
    fun initView() {
        background = ColorDrawable(Color.BLACK)
        rootLy!!.background = background
        imageIv!!.viewTreeObserver
            .addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
                override fun onPreDraw(): Boolean {
                    imageIv!!.viewTreeObserver.removeOnPreDrawListener(this)
                    val location = IntArray(2)
                    imageIv!!.getLocationOnScreen(location)
                    left = iSrcX - location[0]
                    top = iSrcY - location[1]
                    scaleX = iSrcWidth * 1.0f / imageIv!!.width
                    scaleY = iSrcHeight * 1.0f / imageIv!!.height
                    activityEnterAnim()
                    return true
                }
            })
        Glide.with(this).load(iSrcUri).into(imageIv!!)

        imageIv.setOnClickListener {
            activityExitAnim(Runnable {
                finish()
                overridePendingTransition(0, 0)
            })
        }
    }

    override fun onBackPressed() {
        activityExitAnim(Runnable {
            finish()
            overridePendingTransition(0, 0)
        })
    }

    private fun activityEnterAnim() {
        imageIv!!.pivotX = 0f
        imageIv!!.pivotY = 0f
        imageIv!!.scaleX = scaleX
        imageIv!!.scaleY = scaleY
        imageIv!!.translationX = left.toFloat()
        imageIv!!.translationY = top.toFloat()
        imageIv!!.animate().scaleX(1f).scaleY(1f).translationX(0f).translationY(0f).setDuration(300)
            .setInterpolator(DecelerateInterpolator()).start()
        val objectAnimator = ObjectAnimator.ofInt(background, "alpha", 0, 255)
        objectAnimator.interpolator = DecelerateInterpolator()
        objectAnimator.duration = 300
        objectAnimator.start()
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private fun activityExitAnim(runnable: Runnable) {
        imageIv!!.pivotX = 0f
        imageIv!!.pivotY = 0f
        imageIv!!.animate().scaleX(scaleX).scaleY(scaleY).translationX(left.toFloat())
            .translationY(top.toFloat()).withEndAction(runnable).setDuration(300)
            .setInterpolator(DecelerateInterpolator()).start()
        val objectAnimator = ObjectAnimator.ofInt(background, "alpha", 255, 0)
        objectAnimator.interpolator = DecelerateInterpolator()
        objectAnimator.duration = 300
        objectAnimator.start()
    }
}